package com.woniu.car.controller;


import com.woniu.car.common.JWTUtil;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.parameter.ReceiverParam;
import com.woniu.car.parameter.UpdateReceiverParam;
import com.woniu.car.service.TReceiverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * <p>
 * 收获地址表 前端控制器
 * </p>
 *
 * @author zhoubinbin
 * @since 2021-06-21
 */
@RestController
@RequestMapping("/tReceiver")
@Api(tags = "用户收获地址接口")
@Slf4j
public class TReceiverController {
    @Resource
    private TReceiverService receiverService;
    @ApiOperation(value = "收获地址新增")
    //新增收获地址
    @PostMapping("/addReceiver")
    public ResponseEntity addReceiver(@Valid @RequestBody ReceiverParam receiverParam, BindingResult bindingResult, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        if (token==null){
            throw new Exception("您当前未登陆，无法进行该操作");
        }
        String id = JWTUtil.getId(token);
        System.out.println(id);
        Integer userId = Integer.valueOf(id);
        log.info("新增收获地址");
        for (ObjectError error : bindingResult.getAllErrors()) {
            return new ResponseEntity(500,"参数错误",error.getDefaultMessage());
        }
        return receiverService.addReceiver(receiverParam,userId);
    }


    @ApiOperation(value = "收获地址删除")
    //删除收获地址
    @DeleteMapping("deleteReceiver")
    public ResponseEntity deleteReceiver(Integer receiverId, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        if (token==null){
            throw new Exception("您当前未登陆，无法进行该操作");
        }
        String id = JWTUtil.getId(token);
        System.out.println(id);
        Integer userId = Integer.valueOf(id);
        return receiverService.deleteReceiver(receiverId,userId);
    }

    @ApiOperation(value = "展示当前用户的所有收货地址")
    @GetMapping("/selectReceiverAll")
    public ResponseEntity selectReceiverAll(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        if (token==null){
            throw new Exception("您当前未登陆，无法进行该操作");
        }
        String id = JWTUtil.getId(token);
        System.out.println(id);
        Integer userId = Integer.valueOf(id);
        log.info("收货地址删除{}");
        return receiverService.selectReceiverAll(userId);
    }

    @ApiOperation(value = "收获地址修改")
    @PutMapping("updateReceiver")
    public ResponseEntity updateReceiver(@Valid @RequestBody UpdateReceiverParam receiverParam, BindingResult bindingResult, HttpServletRequest request) throws Exception {
        for (ObjectError error : bindingResult.getAllErrors()) {
            return new ResponseEntity(500,"参数错误",error.getDefaultMessage());
        }
        String token = request.getHeader("token");
        if (token==null){
            throw new Exception("您当前未登陆，无法进行该操作");
        }
        String id = JWTUtil.getId(token);
        System.out.println(id);
        Integer userId = Integer.valueOf(id);
        return receiverService.updateReceiver(receiverParam,userId);
    }
    @ApiOperation(value = "将该地址设置为默认地址")
    @PutMapping("updateDefaultAddr")
    public ResponseEntity updateDefaultAddr(Integer receiverId, HttpServletRequest request ) throws Exception {
        String token = request.getHeader("token");
        if (token==null){
            throw new Exception("您当前未登陆，无法进行该操作");
        }
        String id = JWTUtil.getId(token);
        System.out.println(id);
        Integer userId = Integer.valueOf(id);
        return receiverService.addDefaultAddr(receiverId,userId);
    }
}

